Typical Usage:
Typical settings and usage of Byteflight bean
(1) Sending data
Assume the bean name "BF1"
and following bean settings: Interrupt service/event - enabled, Transmit buffers - enabled with Transmit buffers count = 1.
Event OnFreeTxBuf is called at the end of data transmission. Parameter BufferNum indicates the buffer that became empty. Method SendFrame is used for data sending.
Only one transmit buffer is enabled with number 15. This number will be passed as a parameter of SendFrame method.
The example demonstrates sending of 5-byte message with identifier 68.
MAIN.C
#define MY_TX_BUF 15
#define MY_MSG_ID 68
byte flags;
byte err;
byte Data[5] = {1,2,3,4,5};
void main(void)
{
:
/* Clear "Complete" flag */
flags = 0;
/* Send message through buffer 15, with identifier 68 */
err=BF1_SendFrame(MY_TX_BUF,MY_MSG_ID,5,&Data);
if(err!=ERR_OK) {
/* Handle an error */
}
/* Wait for communication complete */
while(!flags) ;
:
}
EVENTS.C
extern byte flags;
void BF1_OnFreeTxBuf(byte BufferNum)
{
flags = 1; /* Set "Complete" flag */
}
(2) Receiving data with FIFO
Assume the bean name "BF1"
and following bean settings: Interrupt service/event - enabled, Receive FIFO - enabled, Size of FIFO = 5,
FIFO identifier acceptance mask = 0xFF (all ID will be accepted), FIFO identifier rejection mask = 0xFF (no ID will be rejected).
Event OnNotEmptyRx is called always, when there are data received in FIFO. Method ReadFrame is used for data reading from FIFO with parameter 'BufferNum' = 0. The method must be called in OnNotEmptyRx event to empty FIFO, otherwise the interrupt will be pending permanently.
The example demonstrates receiving a message with FIFO.
MAIN.C
byte flags;
byte Data[12];
byte id;
byte length;
void main(void)
{
:
/* Clear "Complete" flag */
flags = 0;
/* Wait for communication complete */
while(!flags) ;
/* Now Data field contains received message */
:
}
EVENTS.C
#define FIFO_BUF 0
extern byte flags;
extern byte Data[12];
extern byte id;
extern byte length;
byte err;
void BF1_OnNotEmptyRx(void)
{
/* Read message from FIFO */
err=BF1_ReadFrame(FIFO_BUF,&id,&length,&Data);
if(err!=ERR_OK) {
/* Handle an error */
}
flags = 1; /* Set "Complete" flag */
/* Process received message */
}
(3) Receiving data with dedicated receive buffers
Assume the bean name "BF1"
and following bean settings: Interrupt service/event - enabled, Receive FIFO - disabled, Dedicated receive buffers - enabled,
Receive identifier init may specify the ID of the message, that will be captured in this buffer.
Event OnFullRxBuf is called always, when there is message received in dedicated receive buffer. Parameter 'BufferNum' of the event contains a number of the buffer, which caused coming of the event.
Method ReadFrame is used for data reading from dedicated receive buffer determined by the parameter 'BufferNum'.
Identifier of the message that is captured by certain dedicated receive buffer may by changed with SetAcceptanceCode method. This method may be called only in disabled state.
The example demonstrates receiving a message with dedicated receive buffer. The identifier of the message is 68.
MAIN.C
#define MY_RECV_BUF 0
#define MY_MSG_ID 68
byte flags;
byte Data[12];
byte id;
byte length;
byte RxBufNum;
byte err;
void main(void)
{
:
/* Disable the bean */
(void)BF1_Disable();
/* Set acceptance ID of the buffer 0 to 68 */
/* If the value is specified in property 'Receive identifier init', */
/* the method need not to be called */
err = BF1_SetAcceptanceCode(MY_RECV_BUF,MY_MSG_ID)
if(err!=ERR_OK) {
/* Handle possible error */
}
/* Clear "Complete" flag */
flags = 0;
/* Enable the bean */
(void)BF1_Enable();
/* Wait for communication complete */
while(!flags) ;
/* Read message from the buffer */
err=BF1_ReadFrame(RxBufNum,&id,&length,&Data);
if(err!=ERR_OK) {
/* Handle an error */
}
/* Now Data field contains received message */
:
}
EVENTS.C
extern byte flags;
extern byte RxBufNum;
void BF1_OnFullRxBuf(byte BufferNum)
{
/* Store the number of buffer with new message*/
RxBufNum = BufferNum;
flags = 1; /* Set "Complete" flag */
}
(4) Sending data without interrupts
Assume the bean name "BF1"
and following bean settings: Interrupt service/event - Disabled, Transmit buffers - enabled with Transmit buffers count = 1.
Method SendFrame is used for data sending.
Only one transmit buffer is enabled with number 15. This number will be passed as a parameter of SendFrame method.
Finish of transmission can be determined by polling of GetBufState method, which returns the state of buffer full/free flag.
The example demonstrates sending of 5-byte message with identifier 68.
MAIN.C
#define MY_TX_BUF 15
#define MY_MSG_ID 68
byte err;
byte Data[5] = {1,2,3,4,5};
void main(void)
{
:
/* Send message through buffer 15, with identifier 68 */
err=BF1_SendFrame(MY_TX_BUF,MY_MSG_ID,5,&Data);
if(err!=ERR_OK) {
/* Handle an error */
}
/* Wait for communication complete */
while(!(BF1_GetBufState(MY_TX_BUF))) ;
:
}
(5) Receiving data with FIFO without interrupts
Assume the bean name "BF1"
and following bean settings: Interrupt service/event - disabled, Receive FIFO - enabled, Size of FIFO = 5,
FIFO identifier acceptance mask = 0xFF (all ID will be accepted), FIFO identifier rejection mask = 0xFF (no ID will be rejected).
A new message stored in FIFO can be determined by polling GetStatus method, which returns various status flags (see User Types).
Method ReadFrame is used for data reading from FIFO with parameter 'BufferNum' = 0.
The example demonstrates receiving a message with FIFO in polling mode.
MAIN.C
#define FIFO_BUF 0
byte Data[12];
byte id;
byte length;
byte err;
BF1_TStatus statFlg;
void main(void)
{
:
do {
/* Test the status flags */
(void)GetStatus(&statFlg);
/* Wait for reception of the message */
}while(!statFlg.statusName.RcvFIFO);
/* Read message from FIFO */
err=BF1_ReadFrame(FIFO_BUF,&id,&length,&Data);
if(err!=ERR_OK) {
/* Handle an error */
}
/* Now Data field contains received message */
:
}
(6) Receiving data with dedicated receive buffers without interrupts
Assume the bean name "BF1"
and following bean settings: Interrupt service/event - disabled, Receive FIFO - disabled, Dedicated receive buffers - enabled,
Receive identifier init may specify the ID of the message, that will be captured in this buffer.
Receiving of a new message can be determined by polling of GetBufState method, which returns the state of buffer full/free flag.
Method ReadFrame is used for data reading from dedicated receive buffer determined by the parameter 'BufferNum'.
Identifier of the message that is captured by certain dedicated receive buffer may by changed with SetAcceptanceCode method. This method may be called only in disabled state.
The example demonstrates receiving a message with dedicated receive buffer. The identifier of the message is 68.
MAIN.C
#define MY_RECV_BUF 0
#define MY_MSG_ID 68
byte Data[12];
byte id;
byte length;
byte err;
void main(void)
{
:
/* Disable the bean */
(void)BF1_Disable();
/* Set acceptance ID of the buffer 0 to 68 */
/* If the value is specified in property 'Receive identifier init', */
/* the method need not to be called */
err = BF1_SetAcceptanceCode(MY_RECV_BUF,MY_MSG_ID)
if(err!=ERR_OK) {
/* Handle possible error */
}
/* Enable the bean */
(void)BF1_Enable();
/* Wait for message reception */
while(!(BF1_GetBufState(MY_RECV_BUF))) ;
/* Read message from the buffer */
err=BF1_ReadFrame(MY_RECV_BUF,&id,&length,&Data);
if(err!=ERR_OK) {
/* Handle an error */
}
/* Now Data field contains received message */
:
}
For more about typical usage of the bean code please refer to the page Bean Code Typical Usage.
|